package org.foreningsgatan.modules.glcore.graphics;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.media.opengl.GL;

import org.foreningsgatan.modular.common.logging.Log;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureIO;

/**
 * Copyright (c) 2008 Joakim Back
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * 
 * @author Joakim Back
 * 
 */
public class TextureManager {
	private Map<Long, Texture> textures;

	private Long idCounter = 0L;

	private Log log = new Log(this.getClass());
	
	private PackedTextureLoader packedTextureLoader;

	public TextureManager() {
		textures = new HashMap<Long, Texture>();
		packedTextureLoader = new PackedTextureLoader();
	}
	
	public PackedTextureLoader getPackedTextureLoader() {
		return packedTextureLoader;
	}
	
	public void packTextures() throws IOException {
		packedTextureLoader.pack(this);
	}

	private void loadTexture(Texture pTex) {
		idCounter++;

		pTex.setTexParameterf(GL.GL_TEXTURE_MIN_FILTER,
				GL.GL_LINEAR_MIPMAP_LINEAR);
		pTex.setTexParameterf(GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);

		pTex.setTexParameteri(GL.GL_TEXTURE_WRAP_S, GL.GL_REPEAT);
		pTex.setTexParameteri(GL.GL_TEXTURE_WRAP_T, GL.GL_REPEAT);

		textures.put(idCounter, pTex);
	}

	public long loadTexture(File file) throws IOException {
		loadTexture(TextureIO.newTexture(file, true));

		return idCounter;
	}

	public long loadTexture(BufferedImage im) throws IOException {
		loadTexture(TextureIO.newTexture(im, true));

		return idCounter;
	}

	public long loadTexture(InputStream is, String fileSuffix)
			throws IOException {
		loadTexture(TextureIO.newTexture(is, true, fileSuffix));

		return idCounter;
	}

	public Texture getTexture(Long id) {
		if (textures.containsKey(id)) {
			return textures.get(id);
		} else {
			log.warning("Texture '%s' not found", id);

			return textures.get(1L);
		}
	}

	public void destroy() {
		for (Texture tex : textures.values()) {
			tex.dispose();
		}
	}
}
